﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Configuration;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqUserRole
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);

        public List<UserRole> GetUserRoles(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<RolesInUserTypeEntity> query = db.RolesInUserTypeEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "RoleId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.RoleId);
                            else
                                query = query.OrderByDescending(c => c.RoleId);
                            break;
                        //case "Name":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.Name);
                        //    else
                        //        query = query.OrderByDescending(c => c.Name);
                        //    break;
                    }
                }
                return query.Select(c => Mapper.ToUserRoleObject(c)).ToList();
            }
        }

        public List<UserRole> GetUserRolesForUserType(int UserTypeId,int pageNo)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<RolesInUserTypeEntity> query = db.RolesInUserTypeEntities;
                //return query.Select(c => Mapper.ToUserRoleObject(c => c.UserTypeId==UserTypeId)).ToList();
                query = from roles in db.RolesInUserTypeEntities
                        where roles.UserTypeId ==UserTypeId
                        select roles;

                var pageData = query.Skip((pageNo - 1) * pageSize).Take(pageSize);

                return pageData.Select(c => Mapper.ToUserRoleObject(c)).ToList();

            }
        }

        public List<short> GetUserTypesOfRolesForUser(Guid userId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {

                List<short> query = (from roles in db.RolesInUserTypeEntities
                             join ru in db.aspnet_UsersInRoles on roles.RoleId equals ru.RoleId
                             where ru.UserId == userId
                             select roles.UserTypeId).Distinct().ToList();

                return query;


            }


        
        }

        
        public List<UserRole> GetUserRolesForUserType(int UserTypeId, int pageNo, string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<RolesInUserTypeEntity> query = db.RolesInUserTypeEntities;
                //return query.Select(c => Mapper.ToUserRoleObject(c => c.UserTypeId==UserTypeId)).ToList();
                query = from roles in db.RolesInUserTypeEntities
                        where roles.UserTypeId == UserTypeId
                        select roles;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "RoleId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.RoleId);
                            else
                                query = query.OrderByDescending(c => c.RoleId);
                            break;
                        case "Upadated":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.LastUpdated);
                            else
                                query = query.OrderByDescending(c => c.LastUpdated);
                            break;
                    }
                }

                var pageData = query.Skip((pageNo - 1) * pageSize).Take(pageSize);

                return pageData.Select(c => Mapper.ToUserRoleObject(c)).ToList();

            }
        }

        public List<UserRole> GetUserRolesForUserType(int UserTypeId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<RolesInUserTypeEntity> query = db.RolesInUserTypeEntities;
                //return query.Select(c => Mapper.ToUserRoleObject(c => c.UserTypeId==UserTypeId)).ToList();
                query = from roles in db.RolesInUserTypeEntities
                        where roles.UserTypeId == UserTypeId
                        select roles;      

                return query.Select(c => Mapper.ToUserRoleObject(c)).ToList();

            }
        }

        public UserRole GetUserRole(Guid roleId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToUserRoleObject(db.RolesInUserTypeEntities
                            .SingleOrDefault(p => p.RoleId == roleId));
            }
        }

        public void InsertUserRole(UserRole userRole)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    RolesInUserTypeEntity entity = Mapper.ToUserRoleEntity(userRole);
                    db.RolesInUserTypeEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    userRole.RoleId = entity.RoleId;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to Insert contact " + userRole.RoleId);
                }
            }
        }

        public int UpdateUserRole(UserRole userRole)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    RolesInUserTypeEntity entity = Mapper.ToUserRoleEntity(userRole);

                    db.RolesInUserTypeEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public int DeleteUserRole(UserRole userRole)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    RolesInUserTypeEntity entity = Mapper.ToUserRoleEntity(userRole);
                    db.RolesInUserTypeEntities.Attach(entity, false);
                    db.RolesInUserTypeEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}
